package driving;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jsoup.nodes.Document;

import com.g2room.driving.grap.GenericGrap;
import com.g2room.driving.model.po.Article;
import com.g2room.driving.util.DbUtil;
import com.g2room.driving.util.HtmlUtil;

public class UpdatePublishTime {
	
	public static void main(String[] args) {
		getArtis();
	}
	
	public static void getArtis() {
		List<Article> artis = getUrlData();
		for(Article arti : artis) {
			updatePublishTime(arti.getId(), getPublishTime(arti.getSource(), arti.getSourceUrl()));
		}
	}
	
	public static void updatePublishTime(long id, Date pt) {
		try {
			if(null == pt) return;
			
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String publishTime = format.format(pt);
			Connection connection = DbUtil.getConn();
			Statement statement = connection.createStatement();
			String sql = "update d_article_grap set publish_time = '"+publishTime+"' where id=" + id;
			statement.execute(sql);
			statement.close();
			
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static Date getPublishTime(String source, String url) {
		Document doc = GenericGrap.getDoc(url);
		Date pt = null;
		String publishTime = "";
		String text = "";
		try {
		switch(source) {
			case "开车技巧网":
				text = doc.getElementsByTag("h4").text();
				publishTime = text.substring(text.indexOf("20"), text.indexOf("日") + 1);
				pt = HtmlUtil.string2Date(publishTime, "yyyy年MM月dd日");
				break;
			case "腾讯汽车":
				// 2016-03-04 09:16
				text = doc.getElementsByAttributeValue("class", "a_time").text();
				if(null == text || text.length() == 0) {
					text = doc.getElementsByAttributeValue("class", "article-time").text();
				}
				if(null == text || text.length() == 0) {
					text = doc.getElementsByAttributeValue("class", "pubTime").text();
					//2012年09月25日07:51
					if(!"".equals(text))pt = HtmlUtil.string2Date(text, "yyyy年MM月dd日HH:mm");
				}
				if(null == text || text.length() == 0) {
					text = doc.getElementsByAttributeValue("class", "info").text();
					text = text.substring(text.indexOf("20"), text.indexOf("日") + 6);
					//2012年09月25日07:51
					if(!"".equals(text)) pt = HtmlUtil.string2Date(text, "yyyy年MM月dd日HH:mm");
				}
				
				// info
				if(null == pt) pt = HtmlUtil.string2Date(text, "yyyy-MM-dd HH:mm");
				break;
//			case "汽车之家": 
//				text = doc.getElementsByTag("h4").text();
//				publishTime = text.substring(text.indexOf("20"), text.indexOf("日 ") + 1);
//				break;
//			case "驾校一点通":
//				text = doc.getElementsByTag("h4").text();
//				publishTime = text.substring(text.indexOf("20"), text.indexOf("日 ") + 1);
//				break;
			case "车行天下-360文库" :
				text = doc.getElementById("savedatespan").text();
				pt = HtmlUtil.string2Date(text, "yyyy-MM-dd");
				break;
		}
		} catch (Exception e) {
			System.out.println(text + "==" +url);
		}
		return pt;
	}

	public static List<Article> getUrlData() {
		String sql = "select id,source,source_url from d_article_grap where (publish_time is null or DATE_FORMAT(publish_time, '%Y-%m-%d')=DATE_FORMAT(create_time, '%Y-%m-%d')) and source "
				+ "in ('车行天下-360文库', '腾讯汽车', '开车技巧网')";
		List<Article> articles = new ArrayList<Article>();
		try {
			Connection connection = DbUtil.getConn();
			Statement statement = connection.createStatement();
			ResultSet rs = statement.executeQuery(sql);
			while (rs.next()) {
				Article arti = new Article();
				arti.setId(rs.getLong("id"));
				arti.setSourceUrl(rs.getString("source_url"));
				arti.setSource(rs.getString("source"));
				articles.add(arti);
			}
			rs.close();
			statement.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return articles;
	}
}
